package fr.castorflex.SoundModeWidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.util.Log;
import android.widget.RemoteViews;

public class SoundModeWidget extends AppWidgetProvider {

	public static String CLICK_ON_VIBRATOR = "fr.castorflex.SoundModeWidget.CLICK_VIBRATOR";
	public static String CLICK_ON_MUTEMODE = "fr.castorflex.SoundModeWidget.CLICK_MUTEMODE";

	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub

		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.main_mode);

		Intent active = new Intent(context, SoundModeWidget.class);
		active.setAction(CLICK_ON_MUTEMODE);
		PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
				0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewRingerMode,
				actionPendingIntent);

		active = new Intent(context, SoundModeWidget.class);
		active.setAction(CLICK_ON_VIBRATOR);
		actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
		remoteViews.setOnClickPendingIntent(R.id.imageViewVibratorMode,
				actionPendingIntent);

		IntentFilter filter = new IntentFilter(
				AudioManager.RINGER_MODE_CHANGED_ACTION);
		filter.addAction(AudioManager.VIBRATE_SETTING_CHANGED_ACTION);
		context.getApplicationContext().registerReceiver(this, filter);

		updateImages(context);

		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
		super.onUpdate(context, appWidgetManager, appWidgetIds);

	}

	public void updateImages(Context context) {
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.main_mode);

		if (getIsMuted(context)) {
			remoteViews.setImageViewResource(R.id.imageViewRingerMode,
					R.xml.volume_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewRingerMode,
					R.xml.volume_selector_on);
		}
		if (!getIsVibrate(context)) {
			remoteViews.setImageViewResource(R.id.imageViewVibratorMode,
					R.xml.vibrator_mode_selector_off);
		} else {
			remoteViews.setImageViewResource(R.id.imageViewVibratorMode,
					R.xml.vibrator_mode_selector_on);
		}

		AppWidgetManager manager = AppWidgetManager.getInstance(context);
		ComponentName myWidget = new ComponentName(context,
				SoundModeWidget.class);
		manager.updateAppWidget(myWidget, remoteViews);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		final String action = intent.getAction();

		if (action.equals(CLICK_ON_VIBRATOR)) {
			switchVibrator(context);

		} else if (action.equals(CLICK_ON_MUTEMODE)) {
			switchMuteMode(context);
		} else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)
				|| action.equals(AudioManager.VIBRATE_SETTING_CHANGED_ACTION)) {
			updateImages(context);
		}

		super.onReceive(context, intent);
	}

	public AudioManager getAudio(Context context) {
		return (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
	}

	public void switchMuteMode(Context context) {
		AudioManager audio = getAudio(context);
		if (getIsMuted(context)) {
			audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
		} else {
			if (getIsVibrate(context)) {
				audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
			} else
				audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		}
	}

	public void switchVibrator(Context context) {
		AudioManager audio = getAudio(context);
		if (audio.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
			audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
					AudioManager.VIBRATE_SETTING_ON);
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
					AudioManager.VIBRATE_SETTING_ON);
		} else if (audio.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
			audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
					AudioManager.VIBRATE_SETTING_OFF);
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
					AudioManager.VIBRATE_SETTING_OFF);
		} else if (!getIsVibrate(context)) {
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
					AudioManager.VIBRATE_SETTING_ON);
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
					AudioManager.VIBRATE_SETTING_ON);

		} else {
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION,
					AudioManager.VIBRATE_SETTING_OFF);
			audio.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
					AudioManager.VIBRATE_SETTING_OFF);
		}
	}

	public boolean getIsMuted(Context context) {
		AudioManager audio = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		if (audio.getRingerMode() == AudioManager.RINGER_MODE_SILENT
				|| audio.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE)
			return true;

		return false;

	}

	public boolean getIsVibrate(Context context) {
		AudioManager audio = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);

		if (audio.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE)
			return true;
		if (audio.getRingerMode() == AudioManager.RINGER_MODE_SILENT)
			return false;
		if (audio.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_ON
				&& audio.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION) == AudioManager.VIBRATE_SETTING_ON)
			return true;

		return false;
	}

}
